<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addvisible = true">添加分类</el-button>
      </div>
      <el-table
      stripe
      border
      :data="catelist"
      style="width: 100%">
      <el-table-column
        width="50"
        type=index
        label="序号">
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类名称">
      </el-table-column>
      <el-table-column
        prop="cate_alias"
        label="分类别名">
      </el-table-column>
       <el-table-column
        label="操作">
        <template slot-scope="{ row }">
          <el-button size="mini" type="primary" @click="changeDate(row)">修改</el-button>
          <el-button size="mini" type="danger" @click="remove(row.id)">删除</el-button>
        </template>

      </el-table-column>
    </el-table>
    </el-card>
    <el-dialog
      title="添加文章分类"
      :visible.sync="addvisible"
      :close-on-click-modal="false"
      @closed='$refs.addForm.resetFields()'
      width="35%">
      <!-- 添加的表单 -->
      <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="70px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name" minlength="1" maxlength="10"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias" minlength="1" maxlength="15"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini"  @click="addvisible = false">取 消</el-button>
        <el-button size="mini"  type="primary" @click="hSubmit">确 定</el-button>
      </span>
    </el-dialog>

     <el-dialog
        title="修改文章分类"
        :visible.sync="editvisible"
        :close-on-click-modal="false"
        @closed='$refs.editForm.resetFields()'
        width="35%">
        <!-- 修改的表单 -->
        <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="70px">
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="editForm.cate_name" minlength="1" maxlength="10"></el-input>
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="editForm.cate_alias" minlength="1" maxlength="15"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button size="mini"  @click="editvisible = false">取 消</el-button>
          <el-button size="mini"  type="primary" @click="goChange">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      catelist: [],
      addvisible: false,
      editvisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {}
    }
  },
  methods: {
    async initCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      // console.log(res)
      if (res.code !== 0) return
      this.catelist = res.data
    },
    hSubmit() {
      this.addvisible = false
      this.$refs.addForm.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.initCateList()
      })
    },
    async remove(id) {
      // console.log(row.id)
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除这个分类！')
      const fs = await this.$confirm('此操作将永久删除该分类, 是否继续?', '删除文章分类', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (fs === 'cancel') return
      // this.$message({
      //   type: 'info',
      //   message: '已取消删除'
      // })
      const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.initCateList()
      // console.log(res)

      // this.catelist = this.catelist.splice(index, 1)
      // if (id === 1 || id === 2) return this.$message.error('管理员不允许删除这个分类！')
    },
    changeDate(row) {
      if (row.id === 1 || row.id === 2) return this.$message.error('管理员不允许修改这个分类！')
      this.editvisible = true
      this.editForm = row
      // console.log(row)
    },
    goChange() {
      this.editvisible = false
      this.$refs.editForm.validate(async valid => {
        // console.log(valid)
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.initCateList()
      })
    }
  },
  created() {
    this.initCateList()
  }

}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
